<?php

namespace app\admin\controller;

use app\admin\BaseController;

class AdminController extends BaseController
{
    /**
     * @Description 管理员登录
     *
     * @return void
     */
    public function loginAction()
    {
        try{
            $admin = $this->app->adminService->check($this->post['account']);

            if ($admin->password != md5($this->post['password'])) {
                throw new \Exception('密码不正确', 500);
            }
            $token = $this->app->adminService->makeToken($admin->adminId);
            $data = [
                'token'=>$token->token,
                'account'=>$admin->account,
                'realName'=>$admin->realName
            ];
            return json(['code' => 200, 'msg' => '登录成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
    /**
     * @Description 管理员退出
     *
     * @return void
     */
    public function logoutAction()
    {
        try {
            $this->app->adminService->expiryToken($this->token);

            return json(['code' => 200, 'msg' => '退出成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 管理员列表
     *
     * @return void
     */
    public function listAction()
    {
        try{
            $post = $this->post;

            $params['order'] = ['adminId'=>'desc'];

            $params['where'][] = ['is_delete','=',0];

            if(!empty($post['account']) ){
                $params['where'][] = ['account','like','%'.$post['account'].'%'];
            }
            if(!empty($post['name']) ){
                $params['where'][] = ['realName','like','%'.$post['name'].'%'];
            }
            if(isset($post['status']) && $post['status']!==""){
                $params['where'][] =['status','=',$post['status']];
            }
            $res = $this->app->adminService->list($params,$this->page,$this->pagesize);

            $data['lists'] = [];
            foreach($res as $key=>$vo){
                $arr = [];
                $arr['adminId'] = $vo->adminId;
                $arr['account'] = $vo->account;
                $arr['realName'] = $vo->realName;
                $arr['status'] = $vo->status;
                $arr['statusText'] = $vo->getAdminStatusText();
                $data['lists'][] = $arr;
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($res);

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }


    /**
     * @Description 管理员添加
     *
     * @return void
     */
    public function addAction()
    {
        try{
            $admin = $this->app->adminService->getAdminByAccount($this->post['account']);
            if($admin){
                throw new \Exception("账号已存在，请勿重复添加",500);
            }
            $admin = $this->app->adminService->add($this->post);

            return json(['code' => 200, 'msg' => '添加成功', 'data'=> []]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 管理员详情
     * @param  [type] $id
     *
     * @return void
     */
    public function showAction()
    {
        try{
            $data['adminId'] = $this->admin->adminId;
            $data['realName'] = $this->admin->realName;
            $data['account'] = $this->admin->account;

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 管理员详情
     * @param  [type] $id
     *
     * @return void
     */
    public function infoAction($id)
    {
        try{
            $admin = $this->app->adminService->getAdminById($id);
            if(!$admin){
                throw new \Exception("用户不存在",500);
            }
            $data['adminId'] = $admin->adminId;
            $data['realName'] = $admin->realName;
            $data['account'] = $admin->account;
            $data['status'] = $admin->status;
            $data['statusText'] = $admin->getAdminStatusText();

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
    /**
     * @Description 管理员修改
     *
     * @return void
     */
    public function updateAction($id)
    {
        try{
            $admin = $this->app->adminService->getAdminById($id);
            if(!$admin){
                throw new \Exception("用户不存在",500);
            }
            $this->app->adminService->update($admin,$this->post);

            return json(['code' => 200, 'msg' => '修改成功', 'data'=> []]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
    /**
     * @Description 管理员删除
     * @param  [type] $id
     *
     * @return void
     */
    public function deleteAction($id)
    {
        try{
            $admin = $this->app->adminService->getAdminById($id);
            if (!$admin) {
                throw new \Exception("用户不存在",500);
            }
            $this->app->adminService->delete($admin);

            return json(['code' => 200, 'msg' => '删除成功', 'data'=> []]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
}